<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>后台管理</title>
	<link rel="stylesheet" th:href="@{/static/layui/css/layui.css}" media="all"/>
	<link rel="stylesheet" th:href="@{/static/css/footer.css}" media="all"/>
	<link rel="stylesheet" th:href="@{/static/css/thesame.css}" media="all"/>
	<link rel="stylesheet" th:href="@{/static/css/tree.css}" media="all"/>
	<link rel="stylesheet" th:href="@{/static/css/popCss.css}" media="all"/>
	<link rel="stylesheet" th:href="@{/static/mealUser/css/user.css}"  media="all"/>
	<style type="text/css">
        /*取消选中效果*/
		div{
	    -moz-user-select:none;/*火狐*/
	    -webkit-user-select:none;/*webkit浏览器*/
	    -ms-user-select:none;/*IE10*/
	    -khtml-user-select:none;/*早期浏览器*/
	      user-select:none;
		}
		.layui-form-checkbox[lay-skin="primary"]{padding-left: 0px;}
		.add-mess-child .tree-con-box .tree-one{height: 240px;}
    </style>
</head>
<script th:inline="javascript">var basePath = [[${#httpServletRequest.getContextPath()}]];</script>
<body>
<div class="add-title pad25px"><span style="cursor: pointer;"  onclick="returnUserList()" class="mr10px"><img th:src="@{/static/img/return_icon.png}"></span><span id="actionTitle">新建机构</span></div>
<div class="new-add-box new-add-box-width bgWhite box-shadows border-radius4">
	<div class="new-add-box-scroll">
		<div class="add-mess add-mess-mb mar25px">
			<article style="margin-bottom: 100px">
				<input type="hidden" name="userNo"/>
				<div class="add-mess-child">
					<div>
						<label><em class="red">*</em>供餐机构：</label>
					</div>
					<div class="add-mess-form">
						<p><input type="text" style="width: 280px;" maxlength="30" onkeyup="value=value.replace(/[~!`?\/<>|@#$%^&*()+_=:]/g,'')" name="mealName" placeholder="请输入"></p>
					</div>
				</div>
				<div class="add-mess-child">
					<div>
						<label><em class="red">*</em>登录账号：</label>
					</div>
					<div class="add-mess-form">
						<p><input type="text" style="width: 280px;" maxlength="20" onkeyup="value=value.replace(/[~!`?\/<>|@#$%^&*()+_=:]/g,'')" name="loginName" placeholder="请输入"></p>
					</div>
				</div>
				<div class="add-mess-child">
					<div>
						<label><em class="red">*</em>用户名：</label>
					</div>
					<div class="add-mess-form">
						<p><input type="text" style="width: 280px;" maxlength="10" onkeyup="value=value.replace(/[~!`?\/<>|@#$%^&*()+_=:]/g,'')" name="userName" placeholder="请输入"></p>
					</div>
				</div>
				<div class="add-mess-child">
					<div>
						<label><em class="red">*</em>手机号码：</label>
					</div>
					<div class="add-mess-form">
						<p><input type="text" style="width: 280px;" maxlength="11" name="phone" onkeyup="value=value.replace(/[^\d]/g,'')" placeholder="请输入"></p>
					</div>
				</div>
			</article>
			<div id="treeThree" class="add-mess-child">
				<div>
					<label style="position: relative;left: -820px;"><em class="red">*</em>供餐区域：</label>
				</div>
				<div>
					<div class="tree-con-box layui-form border1px" style="top: 0; height: 240px; margin-top: 10px; left: 80px; width: 400px; box-shadow: none;">
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<div class="layui-footer layui-footer-one">
	<div class="btn-con textCenter">
		<span class="btn-one" style="cursor: pointer;"  onclick="returnUserList()">取&nbsp;消</span>
		<span class="btn-two" style="cursor: pointer;"  id="addBtn"  onclick="addUser()">提&nbsp;交</span>
	</div>
</div>


<script type="text/javascript" th:src="@{/static/js/jquery-2.1.3.min.js}" charset="utf-8"></script>
<script type="text/javascript" th:src="@{/static/js/layuiCon.js}" charset="utf-8"></script>
<script type="text/javascript" th:src="@{/static/layui/layui.js}" charset="utf-8"></script>
<script type="text/javascript" th:src="@{/static/js/date.js}" charset="utf-8"></script>
<script type="text/javascript" th:src="@{/static/js/treeThree.js}" charset="utf-8"></script>

<script>

	//layer初始化
	var layer;
	layui.use('layer', function(){
		layer = layui.layer;
	});

	$(function(){
		$('.add-person ul li').click(function(){
			$(this).addClass('cur-green').siblings().removeClass('cur-green');
		})
		//授权机构树初始化
		authOrgInit();
		//修改初始化（用户userNo存在时，绑定修改信息）
		editUserInit();
	})

	/**
	 * 返回列表
	 */
	function returnUserList(){
		window.location.href=basePath+"/mealUser/listPage";
	}

	/**
	 * 授权机构树初始化
	 */
	function authOrgInit(){
		var userNo = '[[${userNo}]]';
		$.ajax({
			type: "post",
			url: basePath + "/org/getGongcanAllAuthOrganization",
			dataType: "json",
			data: {"userNo": userNo},
			success: function (data) {
				var str = "";
				$.each(data.children, function (index, ao) {
					if (ao.isChildExist == '1' && data.children.length > 0) {
						str += '<ul class="tree-one">';
						$.each(ao.children, function (index, ao1) {
							console.log(ao1);
							str += '<li>';
							str += '<div class="tree-children">';
							str += '<div>';

							str += '<p class="mr5px"><input lay-filter="switchTest" type="checkbox" '+(ao1.authType == 0?' ':checked="checked")+' '+(ao1.cnt == 0?' ':disabled="disabled")+' data-state="0" data-parentorgid="' + ao1.parentOrgId + '" id="' + ao1.orgId + '" name="" lay-skin="primary"></p>';
							str += ao1.isChildExist == '1'?'<p class="tree-arrow cursor"><img src="'+ basePath +'/static/user/img/icon_select_down.png"></p><p>' + ao1.orgName + '</p>':'<p style="margin-left: 8px;margin-right: 6px;">' + ao1.orgName + '</p>';
							str += '</div>';
							str += '</div>';
							if (ao1.isChildExist == '1' && ao.children.length > 0) {
								str += '<ul class="tree-two">';
								$.each(ao1.children, function (index, ao2) {
									str += '<li>';
									str += '<div class="tree-children">';
									str += '<div>';
									str += '<p class="mr5px"><input lay-filter="switchTest" type="checkbox" '+(ao2.authType == 0?' ':checked="checked")+' '+(ao2.cnt == 0?' ':disabled="disabled")+' data-state="0" data-parentorgid="' + ao2.parentOrgId + '" id="' + ao2.orgId + '" name="" lay-skin="primary"></p>';
									str += ao2.isChildExist == '1'?'<p class="tree-arrow cursor"><img src="'+ basePath +'/static/user/img/icon_select_down.png"></p><p>' + ao2.orgName + '</p>':'<p style="margin-left: 8px;margin-right: 6px;">' + ao2.orgName + '</p>';
									str += '</div>'
									str += '</div>';
									if (ao2.isChildExist == '1' && ao1.children.length > 0) {
										str += '<ul class="tree-three">';
										$.each(ao2.children, function (index, ao3) {
											str += '<li>';
											str += '<div class="tree-children">';
											str += '<div>';
											str += '<p class="mr5px"><input lay-filter="switchTest" type="checkbox" '+(ao3.authType == 0?' ':checked="checked")+' '+(ao3.cnt == 0?' ':disabled="disabled")+' data-state="0" data-parentorgid="' + ao3.parentOrgId + '" id="' + ao3.orgId + '" name="" lay-skin="primary"></p>';
											str += ao3.isChildExist == '1'?'<p class="tree-arrow cursor"><img src="'+ basePath +'/static/user/img/icon_select_down.png"></p><p>' + ao3.orgName + '</p>':'<p style="margin-left: 8px;margin-right: 6px;">' + ao3.orgName + '</p>';
											str += '</div>';
											str += '</div>';
											if (ao3.isChildExist == '1' && ao2.children.length > 0) {
												str += '<ul class="tree-four">';
												$.each(ao3.children, function (index, ao4) {
													str += '<li>';
													str += '<div class="tree-children">';
													str += '<div>';
													str += '<p class="mr5px"><input lay-filter="switchTest" type="checkbox" '+(ao4.authType == 0?' ':checked="checked")+' '+(ao4.cnt == 0?' ':disabled="disabled")+' data-state="0" data-parentorgid="' + ao4.parentOrgId + '" id="' + ao4.orgId + '" name="" lay-skin="primary"></p>';
													str += ao4.isChildExist == '1'?'<p class="tree-arrow cursor"><img src="'+ basePath +'/static/user/img/icon_select_down.png"></p><p>' + ao4.orgName + '</p>':'<p style="margin-left: 8px;margin-right: 6px;">' + ao4.orgName + '</p>';
													str += '</div>';
													str += '</div>';
													if (ao4.isChildExist == '1' && ao3.children.length > 0) {
														str += '<ul class="tree-five">';
														$.each(ao4.children, function (index, ao5) {
															str += '<li>';
															str += '<div class="tree-children">';
															str += '<div>';
															str += '<p class="mr5px"><input lay-filter="switchTest" type="checkbox" '+(ao5.authType == 0?' ':checked="checked")+' '+(ao5.cnt == 0?' ':disabled="disabled")+' data-state="0" data-parentorgid="' + ao5.parentOrgId + '" id="' + ao5.orgId + '" name="" lay-skin="primary"></p>';
															str += ao5.isChildExist == '1'?'<p class="tree-arrow cursor"><img src="'+ basePath +'/static/user/img/icon_select_down.png"></p><p>' + ao5.orgName + '</p>':'<p style="margin-left: 8px;margin-right: 6px;">' + ao5.orgName + '</p>';
															str += '</div>';
															str += '</div>';
															str += '</li>';
														})
														str += '</ul>';
													}
													str += '</li>';
												})
												str += '</ul>'
											}
											str += '</li>';
										})
										str += '</ul>';
									}
									str += '</li>';
								})
								str += '</ul>';
							}
							str += '</li>';
						});
						str += '</ul>';
					}
					str += '';
					$("#treeThree .tree-con-box").html(str);
					//授权机构树点击功能初始化
					treeThreeInit();
				});
			}
		})
	}

	/**
	 * 判断是否为空
	 */
	function isNull(value) {
		return value == null || value === "" || value === undefined;
	}

	/**
	 * layer msg
	 */
	function layerMsg(msg){
		var layer;
		layui.use(['layer'], function() {
			layer = layui.layer;
			layer.ready(function(){
				layer.msg(msg);
			});
		});
	}

	/**
	 * layer result
	 */
	function layerResult(msg,type){
		var layer;
		layui.use(['layer'], function() {
			layer = layui.layer;
			layer.ready(function(){
				layer.msg(msg, {icon: type, time:1500});
			})
		});
	}

	/**
	 * layer checkbox
	 */
	function checkBoxInit(){
		layui.use(['form'], function() {
			var form = layui.form;
			form.render('checkbox');
		});
	}

	/**
	 * 添加用户
	 */
	function addUser(){
		var userNo = $("input[name=userNo]").val();
		var mealName = $("input[name=mealName]").val();
		var loginName = $("input[name=loginName]").val();
		var userName = $("input[name=userName]").val();
		var phone = $("input[name=phone]").val();
		var post = $("input[name=post]").val();
		var dept = $("input[name=dept]").val();
		var selectParentOrg = $("#selectParentOrg").data("orgid");
		var authOrg = $("#treeThree input[type=checkbox]:checked").map(function(){
			return this.id;
		}).get().join("-");

		if(isNull(loginName)){
			layerMsg("供餐机构不能为空！");
			return;
		}
		if(isNull(loginName)){
			layerMsg("用户账号不能为空！");
			return;
		}
		if(isNull(userName)){
			layerMsg("用户名不能为空！");
			return;
		}
		if(isNull(phone)){
			layerMsg("手机号不能为空！");
			return;
		}
		if (!/^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1})|(19[0-9]{1}))+\d{8})$/.test(phone)){
			layerMsg("请正确填写您的手机号码！");
			return;
		}
		if(isNull(authOrg)){
			layerMsg("供餐区域不能为空！");
			return;
		}

		var args=[];
		args.push(userNo);
		args.push(loginName);
		args.push(userName);
		args.push(phone);
		args.push('');
		args.push('');
		args.push('10');
		args.push(authOrg);
		args.push(mealName)

		if(isNull(userNo)){
			confirmCon("确认要新建机构吗？",addUserCommit, args);
		}else{
			confirmCon("确认要修改机构吗？",addUserCommit, args);
		}


	}

	/**
	 * 确认后提交
	 */
	function addUserCommit(userNo,loginName,userName,phone,post,dept,selectParentOrg,authOrg,mealName){
		console.log("userNo="+userNo);
		console.log("loginName="+loginName);
		console.log("userName="+userName);
		console.log("phone="+phone);
		console.log("post="+post);
		console.log("dept="+dept);
		console.log("selectParentOrg="+selectParentOrg);
		console.log("authOrg="+authOrg);
		$.ajax({
			url:basePath + "/mealUser/save",
			type:"post",
			dataType:"json",
			data:{"userNo":userNo,"userName":userName,"loginName":loginName,"phone":phone,"post":post,"dept":dept,"orgId":selectParentOrg,"authOrg":authOrg,"postType":"2","mealName":mealName},
			beforeSend: function(){
				$("#addBtn").css("pointer-events", "none");
			},
			success:function (data) {
				if(userNo==null||userNo==""){
					if(data.code==200){
						layer.msg("添加成功", {icon: 1, time:1500},function(){
							window.location.href=basePath+"/mealUser/listPage";
						});
					}else{
						layer.msg(data.msg, {icon: 2, time:1500});
					}
				}else{
					if(data.code==200){
						layer.msg("修改成功", {icon: 1, time:1500},function(){
							window.location.href=basePath+"/mealUser/listPage";
						});
					}else{
						layer.msg(data.msg, {icon: 2, time:1500});
					}
				}
				$("#addBtn").css("pointer-events", "auto");
			}
		})
	}

	/**
	 * 修改初始化
	 */
	function editUserInit(){
		var userNo = '[[${userNo}]]';
		if(!isNull(userNo)){
			$.ajax({
				type:"post",
				url:basePath+"/mealUser/getUserById",
				dataType:"json",
				data:{"userNo":userNo},
				success:function(rst){
					console.log(rst);
					$("input[name=userNo]").val(rst.data.USER_NO);
					$("input[name=mealName]").val(rst.data.MEAL_NAME);
					$("input[name=loginName]").val(rst.data.LOGIN_NAME);
					$("input[name=userName]").val(rst.data.USER_NAME);
					$("input[name=phone]").val(rst.data.PHONE);
					$("#actionTitle").html("修改机构");
					$("title").html("机构修改");
				}
			})
		}
	}

</script>
</body>
</html>
